﻿using ReoGrid.Mvvm;
using ReoGrid.Mvvm.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using unvell.ReoGrid;
using unvell.ReoGrid.IO.OpenXML.Schema;
using static RunningCalculator.Utility;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using DataGrid = System.Windows.Controls.DataGrid;
using MessageBox = System.Windows.MessageBox;
using RadioButton = System.Windows.Controls.RadioButton;
using TextBox = System.Windows.Controls.TextBox;

namespace RunningCalculator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        enum DistanceType { Marathon, Half_Marathon, Ten_km, Five_km, Customize };

        double m_targetDistance = 42.195;

        private Action<int> paceSettingUpdateAction = null;
        private WorksheetModel m_worksheetModel = null;
        private ObservableCollection<IRecordModel> m_paceModel = null;
        PaceCalculator paceCalculator = new PaceCalculator();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void PaceAddButton_Click(object sender, RoutedEventArgs e)
        {
            PaceSettingControl paceSettingControl = new PaceSettingControl(paceStackPanel.Children.Count);
            paceSettingUpdateAction += new Action<int>(paceSettingControl.updateActionCallBack);
            paceSettingControl.deleteAction += new Action<int>(paceSettingControlDeleteCallBack);
            paceSettingControl.sectionLengthUpdateAction += new Action(sectionLengthUpdateCallBack);
            paceStackPanel.Children.Add(paceSettingControl);


            paceAddScrollViewer.ScrollToEnd();
            settingPanelScrollViewer.ScrollToEnd();

            updatePaceSettingControl();
        }

        private void ClearPaceButton_Click(object sender, RoutedEventArgs e)
        {
            if(MessageBox.Show("确定清空所有配速设置？", "操作确认", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                paceStackPanel.Children.Clear();
            }
        }

        private void paceSettingControlDeleteCallBack(int ctlIndex)
        {
            paceSettingUpdateAction?.Invoke(ctlIndex);

            if(paceStackPanel.Children.Count > ctlIndex)
                paceStackPanel.Children.RemoveAt(ctlIndex);

            updatePaceSettingControl();
        }

        private void sectionLengthUpdateCallBack()
        {
            updatePaceSettingControl();
        }

        private void distanceSettingRadioButton_Clicked(object sender, RoutedEventArgs e)
        {
            if(sender is RadioButton radioButton)
            {
                customizeDefineDistanceTextBox.IsEnabled = false;
                switch (radioButton.Name)
                {
                    case "fullMarathonRadioButton":
                        m_targetDistance = 42.195;
                        break;
                    case "halfMarathonRadioButton":
                        m_targetDistance = 21.0975;
                        break;
                    case "tenKmRadioButton":
                        m_targetDistance = 10;
                        break;
                    case "fiveKmRadioButton":
                        m_targetDistance = 5;
                        break;
                    case "customizeDefineDistanceRadioButton":
                        customizeDefineDistanceTextBox.IsEnabled = true;
                        if (customizeDefineDistanceTextBox.Text.Trim() != "")
                        {
                            double distance = 0;
                            if (double.TryParse(customizeDefineDistanceTextBox.Text.Trim(), out distance) && distance >= 0)
                            {
                                m_targetDistance = distance;
                            }
                            else
                                m_targetDistance = -1;
                        }
                        else
                            m_targetDistance = -1;
                        break;
                }
                parseDistanceGap();
            }
        }

        private void resetGridControl()
        {
            dataGridControl.Reset();
            m_paceModel = new ObservableCollection<IRecordModel>();
            m_worksheetModel = new WorksheetModel(dataGridControl, typeof(PaceModel), m_paceModel);

            dataGridControl.CurrentWorksheet.SetSettings(WorksheetSettings.View_ShowRowHeader, false);
            dataGridControl.CurrentWorksheet.SetSettings(WorksheetSettings.Edit_AutoFormatCell, false);
            dataGridControl.CurrentWorksheet.SetSettings(WorksheetSettings.Edit_Readonly, true);
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            adjustGridColumnWidth();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            PaceSettingControl paceSettingControl = new PaceSettingControl(0);
            paceSettingControl.lengthTextBox.Text = "42.195";
            paceSettingUpdateAction += new Action<int>(paceSettingControl.updateActionCallBack);
            paceSettingControl.deleteAction += new Action<int>(paceSettingControlDeleteCallBack);
            paceSettingControl.sectionLengthUpdateAction += new Action(sectionLengthUpdateCallBack);
            paceStackPanel.Children.Add(paceSettingControl);

            resetGridControl();
 
            adjustGrid();
            parseDistanceGap();
        }

        private void customizeDefineDistanceTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(sender is TextBox textBox)
            {
                if(textBox.Text.Trim() == "")
                {
                    m_targetDistance = -1;
                    parseDistanceGap();
                }
                else
                {
                    double distance = -1;
                    if (double.TryParse(textBox.Text, out distance))
                    {
                        if (distance >= 1)
                        {
                            m_targetDistance = distance;
                            parseDistanceGap();
                        }
                        else
                        {
                            MessageBox.Show("几百米你捣鼓个der，叔不算!!!", "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
                            textBox.Text = m_targetDistance >= 0 ? m_targetDistance.ToString() : "";
                        }
                    }
                    else
                    {
                        textBox.Text = m_targetDistance >= 0 ? m_targetDistance.ToString() : "";
                    }
                }
            }
        }

        private void parseDistanceGap()
        {
            if (m_targetDistance > 42.195)
            {
                distanceGapGrid_01.Visibility = Visibility.Visible;
                distanceGapGrid_02.Visibility = Visibility.Visible;
                distanceGapGrid_03.Visibility = Visibility.Visible;
                distanceGapGrid_04.Visibility = Visibility.Visible;

                cb_1km.Visibility = Visibility.Visible;
                cb_2km.Visibility = Visibility.Visible;
                cb_3km.Visibility = Visibility.Visible;
                cb_4km.Visibility = Visibility.Visible;
                cb_5km.Visibility = Visibility.Visible;
                cb_6km.Visibility = Visibility.Visible;
                cb_7km.Visibility = Visibility.Visible;
                cb_8km.Visibility = Visibility.Visible;
                cb_9km.Visibility = Visibility.Visible;
                cb_10km.Visibility = Visibility.Visible;
                cb_halfMarathon.Visibility = Visibility.Visible;
                cb_fullMarathon.Visibility = Visibility.Visible;

                distanceGapGrid_01_Border.Visibility = Visibility.Visible;
                distanceGapGrid_02_Border.Visibility = Visibility.Visible;
                distanceGapGrid_03_Border.Visibility = Visibility.Visible;
            }
            else if (m_targetDistance > 21.0975)
            {
                distanceGapGrid_01.Visibility = Visibility.Visible;
                distanceGapGrid_02.Visibility = Visibility.Visible;
                distanceGapGrid_03.Visibility = Visibility.Visible;
                distanceGapGrid_04.Visibility = Visibility.Visible;

                cb_1km.Visibility = Visibility.Visible;
                cb_2km.Visibility = Visibility.Visible;
                cb_3km.Visibility = Visibility.Visible;
                cb_4km.Visibility = Visibility.Visible;
                cb_5km.Visibility = Visibility.Visible;
                cb_6km.Visibility = Visibility.Visible;
                cb_7km.Visibility = Visibility.Visible;
                cb_8km.Visibility = Visibility.Visible;
                cb_9km.Visibility = Visibility.Visible;
                cb_10km.Visibility = Visibility.Visible;
                cb_halfMarathon.Visibility = Visibility.Visible;
                cb_fullMarathon.Visibility = Visibility.Collapsed;

                distanceGapGrid_01_Border.Visibility = Visibility.Visible;
                distanceGapGrid_02_Border.Visibility = Visibility.Visible;
                distanceGapGrid_03_Border.Visibility = Visibility.Visible;
            }
            else if (m_targetDistance > 10.0 || Math.Abs(m_targetDistance - 10.0) < 0.000001)
            {
                distanceGapGrid_01.Visibility = Visibility.Visible;
                distanceGapGrid_02.Visibility = Visibility.Visible;
                distanceGapGrid_03.Visibility = Visibility.Visible;
                distanceGapGrid_04.Visibility = Visibility.Visible;

                cb_1km.Visibility = Visibility.Visible;
                cb_2km.Visibility = Visibility.Visible;
                cb_3km.Visibility = Visibility.Visible;
                cb_4km.Visibility = Visibility.Visible;
                cb_5km.Visibility = Visibility.Visible;
                cb_6km.Visibility = Visibility.Visible;
                cb_7km.Visibility = Visibility.Visible;
                cb_8km.Visibility = Visibility.Visible;
                cb_9km.Visibility = Visibility.Visible;
                cb_10km.Visibility = Visibility.Visible;
                cb_halfMarathon.Visibility = Visibility.Collapsed;
                cb_fullMarathon.Visibility = Visibility.Collapsed;

                distanceGapGrid_01_Border.Visibility = Visibility.Visible;
                distanceGapGrid_02_Border.Visibility = Visibility.Visible;
                distanceGapGrid_03_Border.Visibility = Visibility.Visible;

                if (Math.Abs(m_targetDistance - 10.0) < 0.000001)
                {
                    cb_10km.Visibility = Visibility.Collapsed;
                    distanceGapGrid_03_Border.Visibility = Visibility.Collapsed;
                    distanceGapGrid_04.Visibility = Visibility.Collapsed;
                }
            }
            else if (m_targetDistance > 5.0 || Math.Abs(m_targetDistance - 5.0) < 0.000001)
            {
                distanceGapGrid_01.Visibility = Visibility.Visible;
                distanceGapGrid_02.Visibility = Visibility.Visible;
                distanceGapGrid_03.Visibility = Visibility.Visible;
                distanceGapGrid_04.Visibility = Visibility.Collapsed;

                distanceGapGrid_02_Border.Visibility = Visibility.Visible;

                cb_1km.Visibility = Visibility.Visible;
                cb_2km.Visibility = Visibility.Visible;
                cb_3km.Visibility = Visibility.Visible;
                cb_4km.Visibility = Visibility.Visible;
                cb_5km.Visibility = Visibility.Visible;

                if(m_targetDistance > 9.0)
                {
                    cb_6km.Visibility = Visibility.Visible;
                    cb_7km.Visibility = Visibility.Visible;
                    cb_8km.Visibility = Visibility.Visible;
                    cb_9km.Visibility = Visibility.Visible;
                }
                else if (m_targetDistance > 8.0)
                {
                    cb_6km.Visibility = Visibility.Visible;
                    cb_7km.Visibility = Visibility.Visible;
                    cb_8km.Visibility = Visibility.Visible;
                    cb_9km.Visibility = Visibility.Collapsed;
                }
                else if (m_targetDistance > 7.0)
                {
                    cb_6km.Visibility = Visibility.Visible;
                    cb_7km.Visibility = Visibility.Visible;
                    cb_8km.Visibility = Visibility.Collapsed;
                    cb_9km.Visibility = Visibility.Collapsed;
                }
                else if (m_targetDistance > 6.0)
                {
                    cb_6km.Visibility = Visibility.Visible;
                    cb_7km.Visibility = Visibility.Collapsed;
                    cb_8km.Visibility = Visibility.Collapsed;
                    cb_9km.Visibility = Visibility.Collapsed;
                }
                else
                {
                    distanceGapGrid_03.Visibility = Visibility.Collapsed;

                    cb_6km.Visibility = Visibility.Collapsed;
                    cb_7km.Visibility = Visibility.Collapsed;
                    cb_8km.Visibility = Visibility.Collapsed;
                    cb_9km.Visibility = Visibility.Collapsed;

                    distanceGapGrid_02_Border.Visibility = Visibility.Collapsed;
                }

                
                cb_10km.Visibility = Visibility.Collapsed;
                cb_halfMarathon.Visibility = Visibility.Collapsed;
                cb_fullMarathon.Visibility = Visibility.Collapsed;

                distanceGapGrid_01_Border.Visibility = Visibility.Visible;
                distanceGapGrid_03_Border.Visibility = Visibility.Collapsed;

                if (Math.Abs(m_targetDistance - 5.0) < 0.000001)
                    cb_5km.Visibility = Visibility.Collapsed;
            }
            else if (m_targetDistance > 1.0)
            {
                distanceGapGrid_01.Visibility = Visibility.Visible;
                distanceGapGrid_02.Visibility = Visibility.Visible;
                distanceGapGrid_03.Visibility = Visibility.Collapsed;
                distanceGapGrid_04.Visibility = Visibility.Collapsed;

                distanceGapGrid_01_Border.Visibility = Visibility.Visible;
                distanceGapGrid_02_Border.Visibility = Visibility.Visible;

                cb_1km.Visibility = Visibility.Collapsed;
                cb_2km.Visibility = Visibility.Collapsed;
                cb_3km.Visibility = Visibility.Collapsed;
                cb_4km.Visibility = Visibility.Collapsed;
                cb_5km.Visibility = Visibility.Collapsed;

                if (m_targetDistance > 4.0)
                {
                    cb_1km.Visibility = Visibility.Visible;
                    cb_2km.Visibility = Visibility.Visible;
                    cb_3km.Visibility = Visibility.Visible;
                }
                else if (m_targetDistance > 3.0)
                {
                    cb_1km.Visibility = Visibility.Visible;
                    cb_2km.Visibility = Visibility.Visible;
                    cb_3km.Visibility = Visibility.Visible;
                    cb_4km.Visibility = Visibility.Collapsed;

                    distanceGapGrid_02.Visibility = Visibility.Collapsed;

                    distanceGapGrid_02_Border.Visibility = Visibility.Collapsed;
                }
                else if (m_targetDistance > 2.0)
                {
                    cb_1km.Visibility = Visibility.Visible;
                    cb_2km.Visibility = Visibility.Visible;
                    cb_3km.Visibility = Visibility.Collapsed;
                    cb_4km.Visibility = Visibility.Collapsed;

                    distanceGapGrid_02.Visibility = Visibility.Collapsed;

                    distanceGapGrid_02_Border.Visibility = Visibility.Collapsed;
                }
                else
                {
                    cb_1km.Visibility = Visibility.Visible;
                    cb_2km.Visibility = Visibility.Collapsed;
                    cb_3km.Visibility = Visibility.Collapsed;
                    cb_4km.Visibility = Visibility.Collapsed;

                    distanceGapGrid_02.Visibility = Visibility.Collapsed;

                    distanceGapGrid_02_Border.Visibility = Visibility.Collapsed;
                }


                cb_10km.Visibility = Visibility.Collapsed;
                cb_halfMarathon.Visibility = Visibility.Collapsed;
                cb_fullMarathon.Visibility = Visibility.Collapsed;

                distanceGapGrid_03_Border.Visibility = Visibility.Collapsed;
            }
            else
            {
                distanceGapGrid_01.Visibility = Visibility.Collapsed;
                distanceGapGrid_02.Visibility = Visibility.Collapsed;
                distanceGapGrid_03.Visibility = Visibility.Collapsed;
                distanceGapGrid_04.Visibility = Visibility.Collapsed;

                cb_1km.Visibility = Visibility.Collapsed;
                cb_2km.Visibility = Visibility.Collapsed;
                cb_3km.Visibility = Visibility.Collapsed;
                cb_4km.Visibility = Visibility.Collapsed;
                cb_5km.Visibility = Visibility.Collapsed;
                cb_6km.Visibility = Visibility.Collapsed;
                cb_7km.Visibility = Visibility.Collapsed;
                cb_8km.Visibility = Visibility.Collapsed;
                cb_9km.Visibility = Visibility.Collapsed;
                cb_10km.Visibility = Visibility.Collapsed;
                cb_halfMarathon.Visibility = Visibility.Collapsed;
                cb_fullMarathon.Visibility = Visibility.Collapsed;

                distanceGapGrid_01_Border.Visibility = Visibility.Collapsed;
                distanceGapGrid_02_Border.Visibility = Visibility.Collapsed;
                distanceGapGrid_03_Border.Visibility = Visibility.Collapsed;
            }
        }

        private void adjustGrid(List<int> highlightIndexList = null)
        {
            adjustGridColumnWidth();

            if (highlightIndexList != null)
            {
                foreach (int rowIndex in highlightIndexList)
                {
                    for (int colIndex = 0; colIndex < dataGridControl.CurrentWorksheet.ColumnHeaders.Count(); ++colIndex)
                    {
                        dataGridControl.CurrentWorksheet.Cells[rowIndex, colIndex].Style.BackColor = unvell.ReoGrid.Graphics.SolidColor.Orange;
                    }
                    dataGridControl.CurrentWorksheet.MergeRange(rowIndex, 0, 1, 6);
                }
            }
        }

        private void adjustGridColumnWidth()
        {
            double controlWidth = dataGridControl.ActualWidth;
            int coulumCount = dataGridControl.CurrentWorksheet.ColumnHeaders.Count();

            double width = Math.Floor((controlWidth - dataGridControl.CurrentWorksheet.ColumnHeaders[0].Width) / (coulumCount - 1)) - 2;

            for (int hIndex = 1; hIndex < coulumCount; ++hIndex)
            {
                dataGridControl.CurrentWorksheet.ColumnHeaders[hIndex].Width = (ushort)width;
            }
        }

        private void updatePaceSettingControl()
        {
            double totalSettingDistance = 0;
            for (int i = 0; i < paceStackPanel.Children.Count; ++i)
            {
                if (paceStackPanel.Children[i] is PaceSettingControl paceSettingControl)
                {
                    totalSettingDistance += paceSettingControl.LengthData;
                    if(totalSettingDistance > m_targetDistance)
                        paceSettingControl.warningGrid.Visibility = Visibility.Visible;
                    else
                        paceSettingControl.warningGrid.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)
        {
            if (customizeDefineDistanceRadioButton.IsChecked == true && (m_targetDistance > 1000 || m_targetDistance < 0.1))
            {
                MessageBox.Show("不接受瞎鸡吧距离定义，正确范围0.1km - 1000km!", "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (paceStackPanel.Children.Count <= 0)
            {
                MessageBox.Show("请添加分段配速!", "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            double totalDistance = 0;
            for (int i = 0; i < paceStackPanel.Children.Count; ++i)
            {
                if (paceStackPanel.Children[i] is PaceSettingControl paceSettingControl)
                    totalDistance += paceSettingControl.LengthData;
            }

            if (m_targetDistance > totalDistance)
            {
                string err = "";
                if (fullMarathonRadioButton.IsChecked == true)
                    err = "分段配速总距离小于全马，请继续添加分段配速.";
                else if (halfMarathonRadioButton.IsChecked == true)
                    err = "分段配速总距离小于半马，请继续添加分段配速.";
                else if (tenKmRadioButton.IsChecked == true)
                    err = "分段配速总距离小于10km，请继续添加分段配速.";
                else if (fiveKmRadioButton.IsChecked == true)
                    err = "分段配速总距离小于5km，请继续添加分段配速.";
                else if (customizeDefineDistanceRadioButton.IsChecked == true)
                    err = "分段配速总距离小于自定义距离，请继续添加分段配速.";

                MessageBox.Show(err, "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                resetGridControl();

                Dictionary<Utility.GapCheckedStatus, bool> checkStatusDict = new Dictionary<Utility.GapCheckedStatus, bool>();
                List<SectionSetting> sectionSettingList = new List<SectionSetting>();

                if (cb_1km.IsChecked == true && m_targetDistance >= 1)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_1km_Checked] = true;
                if (cb_2km.IsChecked == true && m_targetDistance >= 2)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_2km_Checked] = true;
                if (cb_3km.IsChecked == true && m_targetDistance >= 3)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_3km_Checked] = true;
                if (cb_4km.IsChecked == true && m_targetDistance >= 4)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_4km_Checked] = true;
                if (cb_5km.IsChecked == true && m_targetDistance >= 5)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_5km_Checked] = true;
                if (cb_6km.IsChecked == true && m_targetDistance >= 6)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_6km_Checked] = true;
                if (cb_7km.IsChecked == true && m_targetDistance >= 7)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_7km_Checked] = true;
                if (cb_8km.IsChecked == true && m_targetDistance >= 8)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_8km_Checked] = true;
                if (cb_9km.IsChecked == true && m_targetDistance >= 9)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_9km_Checked] = true;
                if (cb_halfMarathon.IsChecked == true && m_targetDistance >= 21.0975 || halfMarathonRadioButton.IsChecked == true)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_HalfMarathon_Checked] = true;
                if (cb_fullMarathon.IsChecked == true && m_targetDistance >= 42.195 || fullMarathonRadioButton.IsChecked == true)
                    checkStatusDict[Utility.GapCheckedStatus.Radio_Marathon_Checked] = true;

                double sectionBeginning = 0;
                for (int index = 0; index < paceStackPanel.Children.Count; ++index)
                {
                    if (paceStackPanel.Children[index] is PaceSettingControl paceSettingControl)
                    {
                        SectionSetting sectionSetting = new SectionSetting();
                        sectionSetting.Section.GapBeginning = sectionBeginning;
                        sectionSetting.Section.GapEnd = sectionBeginning + paceSettingControl.LengthData;
                        sectionSetting.Pace = paceSettingControl.PaceData;
                        sectionBeginning += paceSettingControl.LengthData;

                        sectionSettingList.Add(sectionSetting);
                    }
                }

                paceCalculator.Compute(checkStatusDict, sectionSettingList, m_targetDistance, m_paceModel);

                List<int > specialIndexList = new List<int>();
                for(int index=0;index< m_paceModel.Count; ++index)
                {
                    PaceModel dataModel = m_paceModel[index] as PaceModel;
                    if (dataModel.Index == "半马" || dataModel.Index == "全马" || dataModel.Index == "终点")
                        specialIndexList.Add(index);
                }
 
                adjustGrid(specialIndexList);
            }
        }

        private void ExportButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            dialog.Title = "导出";

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //Default header can not export to excel ,so add a row as header
                //中文来说就是他娘的导出的时候表头不跟随数据导出来，也可能是这个控件叔不熟悉，whatever，使用个变通的办法
                //插入一行做头部，但是有个很意外的情况，在使用Insert(0, paceData)第一行插入后，显示在最后一行，即便是更改了RowIndex，第一行仍然显示在最后一行...
                //于是被迫再次折中
                dataGridControl.CurrentWorksheet.InsertRows(0, 1);
                if (dataGridControl.CurrentWorksheet.ColumnHeaders.Count() >= 9)
                {
                    List<string> headerList = new List<string>() { "顺序", "本段起点(km)", "本段终点(km)", "本段长度(km)", "本段配速", "本段用时", "已跑距离(km)", "整体配速", "用时累计" };
                    dataGridControl.CurrentWorksheet.SetRowsHeight(0, 1, 25);
                    int coulumCount = dataGridControl.CurrentWorksheet.ColumnHeaders.Count();
                    for (int colIndex = 0; colIndex < coulumCount; ++colIndex)
                    {
                        dataGridControl.CurrentWorksheet.Cells[0, colIndex].Data = headerList[colIndex];
                        dataGridControl.CurrentWorksheet.Cells[0, colIndex].Style.VAlign = ReoGridVerAlign.Middle;
                        dataGridControl.CurrentWorksheet.Cells[0, colIndex].Style.HAlign = ReoGridHorAlign.Center;
                        dataGridControl.CurrentWorksheet.Cells[0, colIndex].Style.BackColor = new unvell.ReoGrid.Graphics.SolidColor(235, 235, 250);
                    }
                }

                try
                {
                    dataGridControl.Save(dialog.FileName);
                    m_paceModel.RemoveAt(0);
                }
                catch(Exception ex)
                {
                    m_paceModel.RemoveAt(0);
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}